package com.irdstudio.basic.sequence.service.impl;

import com.irdstudio.basic.sequence.service.facade.LimitableSeqService;
import com.irdstudio.basic.sequence.service.impl.support.redis.RedisSequenceGenerator;
import com.irdstudio.basic.sequence.service.util.PatternDescriptorHelper;

/**
 * @description:具备长度限制(Limitable)功能，到达最大值后自动重置
 * 			        适配基于Redis的流水号生成策略
 * @date:2019年2月15日 下午3:01:52
 * @author:hengyh
 */
public class RedisSeqServiceAdapter implements LimitableSeqService {

    private RedisSequenceGenerator redisSequenceGenerator;

    public RedisSeqServiceAdapter(RedisSequenceGenerator redisSequenceGenerator) {
        this.redisSequenceGenerator = redisSequenceGenerator;
    }

    @Override
    public String getSequence(String key) {
        return String.valueOf(redisSequenceGenerator.getRedisSequence(key, Long.MAX_VALUE));
    }

    @Override
    public String getLimitSequence(String cls, Integer length) {
        return String.valueOf(
                redisSequenceGenerator.getRedisSequence(cls, PatternDescriptorHelper.getMaxValueFromSeqLength(length)));
    }

    @Override
    public String getLimitSequence(String cls, Long maxValue) {
        return String.valueOf(redisSequenceGenerator.getRedisSequence(cls, maxValue));
    }
}
